package com.li.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.li.domain.Admin;
import com.li.domain.AdminRole;
import com.li.domain.Employee;
import com.li.service.AdminRoleService;
import com.li.service.AdminService;
import com.li.utlis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    AdminRoleService adminRoleService;

    @RequestMapping("/listAdmin")
    public JSONObject listAdmin(Admin admin, Integer page, Integer limit){
        List<Admin> list=adminService.listPage(admin,page,limit);
        Integer count=adminService.listPageCount(admin);
        return R.listPage(list,count);
    }

    @RequestMapping("/saveOrUpdateAdmin")
    public JSONObject saveOrUpdateAdmin(Admin admin){
        if (admin==null || admin.getUsername()==null){
            return R.getError(admin);
        }
        if (admin.getId()!=null && !"".equals(admin.getId())){
            //修改
            try {
                adminService.updateAdmin(admin);
            }catch (Exception e){
                return R.getError(admin);
            }
        }else{
            //添加
            try {
                adminService.addAdmin(admin);
            }catch (Exception e){
                return R.getError(admin);
            }
        }
        return R.getSuccess();
    }

    @RequestMapping("/updateByIdAdmin")
    public JSONObject updateByIdAdmin(Admin admin){
        try {
            adminService.updateById(admin);
        }catch (Exception e){
            return R.getError(admin);
        }
        return R.getSuccess();
    }

    @RequestMapping("/editRoleByIdAdmin")
    public List<AdminRole> editRoleByIdAdmin(Admin admin){
        return adminRoleService.list(new QueryWrapper<AdminRole>().eq("admin_id",admin.getId()));
    }

    @GetMapping("/editAdmin/{id}")
    public Admin editAdmin(@PathVariable("id") Integer id){
        return adminService.editAdmin(id);
    }

    @RequestMapping("/delAdmin")
    public JSONObject delAdmin(Admin admin){
        adminService.delAdmin(admin);
        return R.getSuccess();
    }

    @RequestMapping("/addAdminRole")
    public JSONObject addAdminRole(Admin admin){
        //添加
        try {
            adminRoleService.addAdminRole(admin);
        }catch (Exception e){
            return R.getError(admin);
        }
        return R.getSuccess();
    }

    /**
     * 头像上传
     * @param uploadFile
     * @param req
     * @return
     */
    @RequestMapping("/uploadAvatar")
    public String uploadAvatar(MultipartFile uploadFile, HttpServletRequest req){
        String path=null;
        try {
            path=adminService.uploadAvatar(uploadFile , req);
        }catch (Exception e){
            return null;
        }
        return path;
    }
}
